package view;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import database.ConectorDB;

@SuppressWarnings("serial")
public class FinestraMenu extends JFrame implements KeyListener {
	
	private ImagePanel jipMenu;
	private JButton jbLogin;
	private JLabel jlInfo;
	private JButton jbRegister;
	private JTextField jtUser;
	private JPasswordField jpPass;
	private ViewSession ventana;
	private ImagePanel jiForm;
	
	public FinestraMenu(final ViewSession ventana, ConectorDB conn){	
	
		this.ventana = ventana;
		
		//Creamos Panel con Im���gen del Men���
		final URL urlFons = this.getClass().getResource("/view/imagenes/fons.jpg");
		jipMenu = new ImagePanel(urlFons);
		jipMenu.setLayout(null);
		
		//A���adir jTextField al Panel
		
		jtUser = new JTextField("");
		jpPass = new JPasswordField("");
		jtUser.addKeyListener(this);
		jpPass.addKeyListener(this);
		
		jtUser.setBounds(400, 485, 200, 20);
		jpPass.setBounds(400, 545, 200, 20);
		
		//Listeners de los Campos
		
		jipMenu.add(jpPass);
		jipMenu.add(jtUser);
		
		//Creamos panel con im���gen del formulario
		final URL urlCampos = this.getClass().getResource("/view/imagenes/campos_form.png");
		jiForm = new ImagePanel(urlCampos);
		jiForm.setLayout(null);
		jiForm.setBounds(371, 430, 275, 180);
		jiForm.setSize(260, 180);
		
		//Creamos label con la informaci���n para login
		jlInfo = new JLabel("Press enter to login.");
		jlInfo.setBounds(440, 630, 275, 30);
		
		
		//Generar im���genes botones
		final URL urlLogin = this.getClass().getResource("/view/imagenes/boto_login.png");
		java.awt.Image Login = getToolkit().createImage(urlLogin);
		final ImageIcon IconoLogin = new ImageIcon(Login);
		
		final URL urlLogino = this.getClass().getResource("/view/imagenes/boto_login_over.png");
		java.awt.Image Logino = getToolkit().createImage(urlLogino);
		final ImageIcon IconoLogino = new ImageIcon(Logino);
		
		final URL urlLoginc = this.getClass().getResource("/view/imagenes/boto_login_click.png");
		java.awt.Image Loginc = getToolkit().createImage(urlLoginc);
		final ImageIcon IconoLoginc = new ImageIcon(Loginc);
		
		final URL urlRegister = this.getClass().getResource("/view/imagenes/boto_register.png");
		java.awt.Image Register = getToolkit().createImage(urlRegister);
		final ImageIcon IconoRegister = new ImageIcon(Register);
		
		final URL urlRegistero = this.getClass().getResource("/view/imagenes/boto_register_over.png");
		java.awt.Image Registero = getToolkit().createImage(urlRegistero);
		final ImageIcon IconoRegistero = new ImageIcon(Registero);
		
		final URL urlRegisterc = this.getClass().getResource("/view/imagenes/boto_register_click.png");
		java.awt.Image Registerc = getToolkit().createImage(urlRegisterc);
		final ImageIcon IconoRegisterc = new ImageIcon(Registerc);
		
		final URL urlIcon = this.getClass().getResource("/view/imagenes/RANZ.png");
		java.awt.Image icon = getToolkit().createImage(urlIcon);
		ImageIcon appIcon = new ImageIcon(icon);
		
		ventana.setIconImage(appIcon.getImage());
		this.setIconImage(appIcon.getImage());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		//Crear botones
		jbLogin = new JButton(IconoLogin);
		jbLogin.setContentAreaFilled(false);
		jbLogin.setBorderPainted(false);
		jbLogin.setFocusPainted(false);
		jbLogin.setBounds(380, 400, 245, 55);
		jbLogin.setToolTipText("Press to get into Login Menu");
		jbRegister = new JButton(IconoRegister);
		jbRegister.setContentAreaFilled(false);
		jbRegister.setFocusPainted(false);
		jbRegister.setBorderPainted(false);
		jbRegister.setBounds(380, 575, 245, 55);
		jbRegister.setToolTipText("Press to get into Register Menu");
		
		//Listeners
		jbLogin.addMouseListener(new MouseListener() {

			public void mouseReleased(MouseEvent e) {
				jbLogin.setIcon(IconoLogino);
			}

			public void mousePressed(MouseEvent e) {
				jbLogin.setIcon(IconoLoginc);
			}

			public void mouseExited(MouseEvent e) {
				jbLogin.setIcon(IconoLogin);
			}

			public void mouseEntered(MouseEvent e) {
				jbLogin.setIcon(IconoLogino);
			}
			
			public void mouseClicked(MouseEvent e) {
				if (!ventana.login(jtUser.getText(), String.valueOf(jpPass.getPassword()))) {
					JOptionPane.showMessageDialog(null, "User or Pasword typed wrong", "ERROR", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		jbRegister.addMouseListener(new MouseListener() {

			public void mouseReleased(MouseEvent e) {
				jbRegister.setIcon(IconoRegistero);
			}

			public void mousePressed(MouseEvent e) {
				jbRegister.setIcon(IconoRegisterc);
			}

			public void mouseExited(MouseEvent e) {
				jbRegister.setIcon(IconoRegister);
			}

			public void mouseEntered(MouseEvent e) {
				jbRegister.setIcon(IconoRegistero);
			}
			
			public void mouseClicked(MouseEvent e) {
				if (!ventana.checkUser(jtUser.getText(), String.valueOf(jpPass.getPassword()))) {
					JOptionPane.showMessageDialog(null, "User already exists", "ERROR", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "You are registered !", "OK", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		
		//A���adir botones a Panel
		jipMenu.add(jbLogin);
		jipMenu.add(jbRegister);
		jipMenu.add(jiForm);
		jipMenu.add(jlInfo);
		
		this.add(jipMenu);
		
		setSize(1024,768);
		setTitle("Menu Ranz");
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			if (!ventana.login(jtUser.getText(), String.valueOf(jpPass.getPassword()))) {
				JOptionPane.showMessageDialog(null, "User or Pasword typed wrong", "ERROR", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {}

	@Override
	public void keyTyped(KeyEvent arg0) {}
	
}


